<?php
/**
 * Eren Framework
 * 
 * DownLoad class
 * 
 * @category System
 * @author Manu Soros Leu
 * @copyright manusoros@gmail.com
 */

class Eren_DownLoad
{
    /**
     * Down Header
     * 
     * @param string $fileName
     * @param strint $charset
     */
    private static function _header($fileName, $charset = 'utf-8', $fileType = 'octet-stream')
    {
        Header("Content-type: application/{$fileType};charset={$charset}");
        Header("Accept-Ranges: bytes");
        Header("Content-Disposition: attachment; filename={$fileName}");
    }
    
    /**
     * DownLoad txt file
     * 
     * @param string $fileName
     * @param strint $content
     * @param strint $charset
     */
    public static function txt($fileName, $content, $charset = 'utf-8')
    {
        self::_header($fileName, $charset);
        echo $content;
    }
    
    /**
     * DownLoad html file
     * 
     * @param string $fileName
     * @param strint $content
     * @param strint $charset
     */
    public static function xls($fileName, $title, $content, $charset = 'gbk')
    {
        self::_header($fileName, $charset, 'vnd.ms-excel');
        $header = <<<header
<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
<meta http-equiv=Content-Type content="text/html; charset=gb2312">
<!--[if gte mso 9]><xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name>{$title}</x:Name>
<x:WorksheetOptions>
<x:DisplayGridlines/>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
</x:ExcelWorkbook>
</xml><![endif]-->
</head>
header;
        $content = Eren_String::convChar("UTF-8", "GBK", $header . $content);
        echo $content;
    }
}